package com.dongdong.service.Impl;
import com.dongdong.constant.RedisUtil;
import cn.hutool.core.util.ObjUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.dongdong.constant.Constant;
import com.dongdong.mapper.TbContentMapper;
import com.dongdong.model.TbContent;
import com.dongdong.model.TbContentExample;
import com.dongdong.service.ContentService;
import com.dongdong.vo.ContentVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ContentServiceImpl implements ContentService {
    @Autowired
    private TbContentMapper tbContentMapper;

    @Autowired
    private StringRedisTemplate redisTemplate; //缓存

    @Override
    public List<ContentVO> findAll(Long id) {
        TbContentExample example = new TbContentExample();
        example.createCriteria().andCategoryIdEqualTo(id);
        List<TbContent> tbContents = tbContentMapper.selectByExample(example);
        List<ContentVO> collect = tbContents.stream().map((x) -> {
            ContentVO contentVO = new ContentVO();
            BeanUtils.copyProperties(x, contentVO);
            return contentVO;
        }).collect(Collectors.toList());

        return collect;
    }

//轮播图+加缓存
    @Override
    public List<ContentVO> findAllredis(Long id) {
        //加入缓存中间件 减少数据库查询次数  这是使用StringRedisTemplate
        Boolean flag = redisTemplate.hasKey(id.toString());

        if (flag){
            //List<ContentVO> contentVOList = JSON.parseObject(jsonStr, new TypeReference<List<ContentVO>>() {});
            Object o = redisTemplate.boundHashOps(id.toString()).get(Constant.ONE);//key
            List<ContentVO> vos = JSON.parseObject(o.toString(), new TypeReference<List<ContentVO>>() {});

            if (ObjUtil.isAllNotEmpty(vos)){
                //如果缓存有就直接返回  没有就查询数据库 然后存入缓存
                return vos;
            }
        }
        List<TbContent> contents = tbContentMapper.selectByExample(null);
        //根据类目id去分组
        Map<Long, List<TbContent>> map = contents.stream().collect(Collectors.groupingBy(TbContent::getCategoryId));
        //根据前端传来的类目id，获取到分组后的集合
        List<ContentVO> centVOS = map.get(id).stream().map(item -> {
            ContentVO contentVO = new ContentVO();
            BeanUtils.copyProperties(item, contentVO);
            return contentVO;
        }).collect(Collectors.toList());

        if(id==1){
            redisTemplate.boundHashOps(RedisUtil.GUANGGAO+id).put(Constant.ONE, JSON.toJSONString(centVOS));
        }else if(id==2) {
            redisTemplate.boundHashOps(RedisUtil.GUANGGAO+id).put(Constant.TWO, JSON.toJSONString(centVOS));
        }else{
            redisTemplate.boundHashOps(RedisUtil.GUANGGAO+id).put(Constant.ONE, JSON.toJSONString(centVOS));
        }


        return centVOS;
    }
















}
